package com.sam.security.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.SecurityConfig;
import org.springframework.security.intercept.web.RequestKey;

import com.sam.security.service.SecurityService;

/**
 * 获取资源路径与权限对应Map
 * 
 * @project EMan
 * @author Sam
 * @version
 * @date Feb 17, 2009 10:49:27 AM
 */
@SuppressWarnings("unchecked")
public class RequestMapFactoryBean implements FactoryBean {

	private LinkedHashMap<RequestKey, ConfigAttributeDefinition> requestMap;
	private SecurityService securityService;

	public Object getObject() throws Exception {
		if (requestMap == null) {
			init();
		}
		return requestMap;
	}

	public Class getObjectType() {
		return LinkedHashMap.class;
	}

	public SecurityService getSecurityService() {
		return securityService;
	}

	public void init() {
		requestMap = new LinkedHashMap<RequestKey, ConfigAttributeDefinition>();
		List<Map<String, Object>> res = securityService.queryRequestMap();
		for (Map<String, Object> map : res) {
			requestMap.put(new RequestKey((String)map.get("resourceString")),
					new ConfigAttributeDefinition(new SecurityConfig((String)(map
							.get("userFunctionName")))));
		}
	}

	public boolean isSingleton() {
		return true;
	}

	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}

}
